package com.asj.house.service.impl;

import com.asj.house.entity.*;
import com.asj.house.mapper.HouseRoomDetailMapper;
import com.asj.house.returns.RespCode;
import com.asj.house.returns.RespEntity;
import com.asj.house.service.*;
import com.asj.house.utils.TimeUtilCustomize;
import com.asj.house.vo.SubscribeVO;
import com.asj.house.vo.result.RoomDetailResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 房源详细信息表 服务实现类
 * </p>
 *
 * @author asj
 * @since 2021-03-04
 */
@Service
public class HouseRoomDetailServiceImpl extends ServiceImpl<HouseRoomDetailMapper, HouseRoomDetail> implements HouseRoomDetailService {

    @Autowired
    private HouseRoomService houseRoomService;

    @Autowired
    private HouseRoomMapService houseRoomMapService;

    @Autowired
    private HouseRoomResourceService houseRoomResourceService;

    @Autowired
    private HouseIntermediaryService houseIntermediaryService;

    @Autowired
    private HouseRoomIntermediaryService houseRoomIntermediaryService;

    @Autowired
    private HouseSubscribeService houseSubscribeService;

    @Override
    public RespEntity<RoomDetailResult> getRoomDetail(Integer roomId) {

        //获取房源基本信息
        HouseRoom houseRoom = getHouseRoom(roomId);

        //获取房源详细信息
        HouseRoomDetail houseRoomDetail = getHouseRoomDetail(roomId);

        //获取房源地图信息
        HouseRoomMap houseRoomMap = getHouseRoomMap(roomId);

        //获取房源建造信息
        HouseRoomResource houseRoomResource = getHouseRoomResource(roomId);

        //获取房源中介信息
        List<HouseIntermediary> houseIntermediaryList = getHouseIntermediary(roomId);

        //获取预约看房记录
        SubscribeVO subscribeVO = getSubscribe(roomId);

        //数据组装
        RoomDetailResult roomDetailResult = new RoomDetailResult();
        roomDetailResult.setHouseRoom(houseRoom);
        roomDetailResult.setHouseRoomDetail(houseRoomDetail);
        roomDetailResult.setHouseRoomMap(houseRoomMap);
        roomDetailResult.setHouseRoomResource(houseRoomResource);
        roomDetailResult.setHouseIntermediaryList(houseIntermediaryList);
        roomDetailResult.setSubscribeVO(subscribeVO);

        return new RespEntity<>(RespCode.SUCCESS, roomDetailResult);
    }

    private HouseRoom getHouseRoom(Integer roomId) {
        QueryWrapper<HouseRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HouseRoom::getId, roomId)
                .eq(HouseRoom::getDeleted, 0);
        return houseRoomService.getOne(queryWrapper);
    }

    private HouseRoomDetail getHouseRoomDetail(Integer roomId) {
        QueryWrapper<HouseRoomDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HouseRoomDetail::getRoomId, roomId)
                .eq(HouseRoomDetail::getDeleted, 0);
        return baseMapper.selectOne(queryWrapper);
    }

    private HouseRoomMap getHouseRoomMap(Integer roomId) {
        QueryWrapper<HouseRoomMap> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HouseRoomMap::getRoomId, roomId)
                .eq(HouseRoomMap::getDeleted, 0);
        return houseRoomMapService.getOne(queryWrapper);
    }

    private HouseRoomResource getHouseRoomResource(Integer roomId) {
        QueryWrapper<HouseRoomResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HouseRoomResource::getRoomId, roomId)
                .eq(HouseRoomResource::getDeleted, 0);
        return houseRoomResourceService.getOne(queryWrapper);
    }

    private List<HouseIntermediary> getHouseIntermediary(Integer roomId) {
        QueryWrapper<HouseRoomIntermediary> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HouseRoomIntermediary::getRoomId, roomId)
                .eq(HouseRoomIntermediary::getDeleted, 0);
        List<HouseRoomIntermediary> houseRoomIntermediaryList = houseRoomIntermediaryService.list(queryWrapper);

        if (houseRoomIntermediaryList.isEmpty()) {
            return new ArrayList<>();
        }

        List<Integer> intermediaryIdList = houseRoomIntermediaryList.stream()
                .map(HouseRoomIntermediary::getIntermediaryId)
                .collect(Collectors.toList());

        return houseIntermediaryService.listByIds(intermediaryIdList);
    }

    private SubscribeVO getSubscribe(Integer roomId) {
        //前30天
        Date dayFromToday = TimeUtilCustomize.getDayFromToday(30);
        String lastMonth = TimeUtilCustomize.parseDateToStr(TimeUtilCustomize.DATE_TIME_FORMAT, dayFromToday);
        //今天
        String today = TimeUtilCustomize.parseDateToStr(TimeUtilCustomize.DATE_TIME_FORMAT, new Date());

        QueryWrapper<HouseSubscribe> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HouseSubscribe::getRoomId, roomId)
                .between(HouseSubscribe::getCreatedDate, lastMonth, today);
        List<HouseSubscribe> houseSubscribeList = houseSubscribeService.list(queryWrapper);

        if (houseSubscribeList.isEmpty()) {
            return new SubscribeVO(null, 0);
        }

        houseSubscribeList = houseSubscribeList.stream()
                .sorted(Comparator.comparing(HouseSubscribe::getCreatedDate).reversed())
                .collect(Collectors.toList());

        LocalDateTime createdDate = houseSubscribeList.get(0).getCreatedDate();
        String date = TimeUtilCustomize.parseLocalDateToStr(TimeUtilCustomize.DATE_TIME_FORMAT, createdDate);

        SubscribeVO subscribeVO = new SubscribeVO();
        subscribeVO.setCount(houseSubscribeList.size());
        subscribeVO.setDate(date);
        return subscribeVO;
    }
}
